package com.song.javathread_day01.ch1_8;

import java.util.Date;
import java.util.Deque;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

public class CleanerTask  extends Thread{

	private Logger logger = Logger.getLogger(CleanerTask.class);
	
	private Deque<Event> deque;

	public CleanerTask(Deque<Event> deque) {
		super();
		this.deque = deque;
		setDaemon(true);
	}

	@Override
	public void run() {
		ch1_8.logger.debug("clean task thread-"+Thread.currentThread().getId()+" is start!");
		while(true)
		{
			Date date = new Date();
			clean(date);
		}
	}

	private void clean(Date date) {
		long difference = 0;
		boolean delete;
		if(deque.size()<=0){
			return;
		}
		delete=false;
		do{
			Event e = deque.getLast();
			difference =date.getTime() - e.getDate().getTime();
			if(difference > 10000)
			{
				ch1_8.logger.debug("cleaner  cleaner a new task！ ");
				deque.removeLast();
				delete = true;
			}
		}while(difference >10000);
		
		if(delete)
		{
			ch1_8.logger.debug("cleaner :size of the queue : "+deque.size() );
		}
			
	}
	
	
	
	
}
